package com.yy.config;

import com.yy.constant.AuthConstant;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

/**
 * 描述: [feign的请求拦截]
 * 作者: yy
 */
@Component
public class FeignInterceptor implements RequestInterceptor {

    /**
     * 1.浏览器------------>A---------------->B
     * @param requestTemplate 即将要发起的新请求对象
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // 当前的请求对象 里面是有token
        HttpServletRequest request = requestAttributes.getRequest();
        requestTemplate.header(AuthConstant.AUTHORIZATION, request.getHeader(AuthConstant.AUTHORIZATION));
    }
}

